package com.veepeak.server.api.app;

import com.alibaba.fastjson.JSONObject;
import com.veepeak.server.common.JSONResult;
import com.veepeak.server.domain.User;
import com.veepeak.server.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/regist")
@Api(description = "注册接口")
public class RegistApi {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取邮箱注册验证码", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "邮箱地址", paramType = "path", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/verifycode")
    public JSONObject emailCodeRegist(String email){
        try {
            String result = userService.emailVerifyCode(email);
            return JSONResult.toSuccessJson(result);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.toFailureJson("fail : "+e.getMessage());
        }
    }

    @PostMapping(value = {"/",""})
    @ApiOperation(value = "用户注册", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "邮箱地址", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", paramType = "param", required = true, dataTypeClass = String.class)
    })
    public JSONObject regist(String email,String password,String verifyCode ){
        try {
            User user = userService.regist(email, password, verifyCode);
            return JSONResult.toSuccessJson(user,"success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail : "+e.getMessage());
        }
    }

    @ApiOperation(value = "获取重置密码邮箱验证码", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "邮箱地址", paramType = "path", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/verifycode/reset")
    public JSONObject emailCodeReset(String email){
        try {
            String result = userService.resetPasswordEmailVerifyCode(email);
            return JSONResult.toSuccessJson(result);
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail : "+e.getMessage());
        }
    }

    @ApiOperation(value = "用户重置密码", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "邮箱地址", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", paramType = "param", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/reset")
    public JSONObject reset(String email, String password,String verifyCode){
        try {
            User user = userService.resetPassword(email,password,verifyCode);
            return JSONResult.toSuccessJson("success");
        } catch (Exception e) {
            return JSONResult.toFailureJson(e.getMessage());
        }
    }
}
